define(['app'], function (app) {
    app.controller('users', function ($scope, $http) {

        function init() {
            $http({
                method: 'GET',
                url: '/OSRS/user/list?pageNow=1&pageSize=5',
            }).then(function (res) {
                $scope.data = res.data.pageDatas;
                $scope.totalRecord = res.data.totalRecords;
                $scope.pageNow = res.data.pageNow;
                $scope.pageSize = res.data.pageSize;
                layui.use(['laypage'], function () {
                    var laypage = layui.laypage;

                    //执行一个laypage实例
                    laypage.render({
                        elem: 'pagination', //注意，这里的 test1 是 ID，不用加 # 号
                        count: $scope.totalRecord,//页总数，从服务端得到
                        curr: $scope.pageNow,
                        limit: $scope.pageSize,
                        jump: function (obj, first) {

                            //首次不执行
                            if (!first) {
                                //obj包含了当前分页的所有参数，比如：
                                $http({
                                    method: 'GET',
                                    url: '/OSRS/user/list?pageNow=' + obj.curr + '&pageSize=' + obj.limit + "&" + $scope.search
                                }).then(function (res) {
                                    $scope.data = res.data.pageDatas;
                                    $scope.totalRecord = res.data.totalRecords;
                                    $scope.pageNow = res.data.pageNow;
                                    $scope.pageSize = res.data.pageSize;
                                });

                            }
                        }
                    });


                })


            });


        }

        $http({
            method: 'GET',
            url: '/OSRS/user/role'
        }).then(function (res) {
            $scope.roles = res.data;

        });

        $http({
            method: 'GET',
            url: '/OSRS/user/list?pageNow=1&pageSize=5'
        }).then(function (res) {

            $scope.data = res.data.pageDatas;
            $scope.totalRecord = res.data.totalRecords;
            $scope.pageNow = res.data.pageNow;
            $scope.pageSize = res.data.pageSize;
            //$scope.search;

            layui.use(['form', 'laypage'], function () {
                window.form = layui.form;
                var laypage = layui.laypage;
                form.render();
                //监听提交
                form.on('submit(formDemo)', function (data) {

                    $scope.search = 'enable=' + data.field.enable + '&roleid=' + data.field.roleid + '&username=' + data.field.username;
                    $http.get('/OSRS/user/list?pageSize=' + $scope.pageSize + '&pageNow=1&' + $scope.search).then(function (res) {
                        $scope.data = res.data.pageDatas;
                        $scope.totalRecord = res.data.totalRecords;
                        $scope.pageNow = res.data.pageNow;
                        $scope.pageSize = res.data.pageSize;

                        //执行一个laypage实例
                        console.log($scope.totalRecord);
                        console.log('-------');
                        laypage.render({
                            elem: 'pagination', //注意，这里的 test1 是 ID，不用加 # 号
                            count: $scope.totalRecord,//页总数，从服务端得到
                            curr: $scope.pageNow,
                            limit: $scope.pageSize,
                            jump: function (obj, first) {

                                    //obj包含了当前分页的所有参数，比如：
                                $http({
                                    method: 'GET',
                                    url: '/OSRS/user/list?pageNow=' + obj.curr + '&pageSize=' + obj.limit + "&" + $scope.search,
                                }).then(function (res) {
                                    $scope.data = res.data.pageDatas;
                                    $scope.totalRecord = res.data.totalRecords;
                                    $scope.pageNow = res.data.pageNow;
                                    $scope.pageSize = res.data.pageSize;
                                });


                                }

                        });

                    });

                    return false;
                });
                form.on('submit(layerform)', function (data) {
                    $http({
                        method: 'POST',
                        url: '/OSRS/user/change',
                        data: JSON.stringify(data.field)
                    }).then(function (res) {
                        console.log(res.data);
                        init();
                       layer.close(NewUserForm);

                    }, function (err) {
                        layer.msg("添加失败");
                    });
                    return false;
                });

                form.on('submit(mod)', function (data) {


                    $http({
                        method: 'POST',
                        url: '/OSRS/user/change',
                        data: JSON.stringify(data.field)
                    }).then(function (res) {
                        console.log(res.data);

                        init();

                        layer.close(oldUserForm);

                    }, function (err) {
                        layer.msg("修改失败");
                    });
                    return false;

                });

                laypage.render({
                    elem: 'pagination', //注意，这里的 test1 是 ID，不用加 # 号
                    count: $scope.totalRecord,//页总数，从服务端得到
                    curr: $scope.pageNow,
                    limit: $scope.pageSize,
                    jump: function (obj, first) {
                        //首次不执行
                        if (!first) {
                        //obj包含了当前分页的所有参数，比如：
                        $http({
                            method: 'GET',
                            url: '/OSRS/user/list?pageNow=' + obj.curr + '&pageSize=' + obj.limit + "&" + $scope.search,
                        }).then(function (res) {
                            $scope.data = res.data.pageDatas;
                            $scope.totalRecord = res.data.totalRecords;
                            $scope.pageNow = res.data.pageNow;
                            $scope.pageSize = res.data.pageSize;
                        });
                        }
                    }
                });
            });
        }, function (err) {
            layer.msg("获取数据失败");
        });
        $scope.all = false;
        $scope.allChk = function () {
            $scope.data.map(function (value) {
                value.chk = $scope.all;
                console.log($scope.data);
                return value
            })
        };

        $scope.enableUser = function (isenable) {
            var r = document.getElementsByName("userid");
            var ids = "";
            for (var i = 0; i < r.length; i++) {
                if (r[i].checked) {
                    ids += r[i].value;
                    ids += ",";
                    if (isenable != "3") {

                        $scope.data[i].state = isenable;

                    }

                }
            }

            if (ids != "") {
                ids = ids.substring(0, ids.length - 1);

                var jsonStr = isenable + ":" + ids;

                $http({
                    method: 'post',
                    url: '/OSRS/user/enable',
                    data: jsonStr
                }).then(function (res) {
                    if (res.data == "ok") {
                        layer.msg("修改成功");
                    }
                });
            }


        };
        $scope.layer_useradd = function () {
            var list = "";

            for (var i = 0; i < $scope.roles.length; i++) {


                var option = '<option value="' + $scope.roles[i].roleid + '">' + $scope.roles[i].rolename + '</option>';


                list = list + option;

            }

            NewUserForm= layer.open({
                type: 1,
                title: "新增用户",
                area: ['640px', '600px'],
                content: `
				<form class="layui-form" id="layerform" action="" method="post">
				  <div class="layui-form-item">
				    <label class="layui-form-label">姓名</label>
				    <div class="layui-input-block">
				      <input type="text" name="username" required  lay-verify="required" placeholder="请输入用户姓名" autocomplete="off" class="layui-input">
				    </div>
				  </div>
				    <div class="layui-form-item">
				    <label class="layui-form-label">职位</label>
				    <div class="layui-input-block">
				      <input type="text" name="position" required  lay-verify="required" placeholder="请输入职位" autocomplete="off" class="layui-input">
				    </div>
				  </div>
				    <div class="layui-form-item">
				    <label class="layui-form-label">电话</label>
				    <div class="layui-input-block">
				      <input type="text" name="phonenumber" required  lay-verify="phone" placeholder="请输入手机号" autocomplete="off" class="layui-input">
				    </div>
				  </div>
				  <div class="layui-form-item">
				    <label class="layui-form-label">邮箱</label>
				    <div class="layui-input-block">
				      <input type="text" name="email" required  lay-verify="email" placeholder="请输入邮箱" autocomplete="off" class="layui-input">
				    </div>
				  </div>
				  <div class="layui-form-item">
				    <label class="layui-form-label">角色</label>
				    <div class="layui-input-block">
				      <select name="roleid" lay-verify="required">
				        <option value="">--请选择角色--</option>
				       ` + list + `
				      </select>
				    </div>
				  </div>
				  <div class="layui-form-item">
				    <label class="layui-form-label">状态</label>
				    <div class="layui-input-block">
				      <input type="radio" name="state" value="1" title="启用" checked>
				      <input type="radio" name="state" value="0" title="禁用">
				    </div>
				  </div>
				  <div class="layui-form-item layui-form-text">
				    <label class="layui-form-label">备注</label>
				    <div class="layui-input-block">
				      <textarea name="describle" placeholder="请输入备注" class="layui-textarea"></textarea>
				    </div>
				  </div>
				  <div class="layui-form-item">
				    <div class="layui-input-block">
				      <button class="layui-btn" lay-submit lay-filter="layerform">立即提交</button>
				      <button type="reset" class="layui-btn layui-btn-primary">重置</button>
				    </div>
				  </div>
				</form>
				`
            })
            form.render();
        }

        $scope.preview = function (x) {
            console.log(x);
            //根据id获取其信息
//			$http({
//				method:"get",
//				url:""
//			}).then(function(res){
//			
//			},function(err){
//			
//			})
            var stateStr;
            if (x.state == '1') {
                stateStr = '<input type="radio" name="state" value="1" checked title="启用" disabled>  <input type="radio" name="state" value="0" title="禁用"  disabled>'
            } else {

                stateStr = '<input type="radio" name="state" value="1" title="启用" disabled>  <input type="radio" name="state" value="0" title="禁用" checked disabled>'
            }

            layer.open({
                type: 1,
                title: "用户预览",
                area: ['640px', '600px'],
                content: `
				<form class="layui-form" id="layerform" action="">
				  <div class="layui-form-item">
				    <label class="layui-form-label">姓名</label>
				    <div class="layui-input-block">
				      <input type="text" name="name" required  lay-verify="required" value="`
                + x.username +
                `" autocomplete="off" class="layui-input" disabled>
				    </div>
				  </div>
				    <div class="layui-form-item">
				    <label class="layui-form-label">职位</label>
				    <div class="layui-input-block">
				      <input type="text" name="position" required  lay-verify="position" value="` +
                x.position
                + `" autocomplete="off" class="layui-input" disabled>
				    </div>
				  </div>
				    <div class="layui-form-item">
				    <label class="layui-form-label">电话</label>
				    <div class="layui-input-block">
				      <input type="text" name="phonenumber" required  lay-verify="phone" value="` +
                x.phonenumber
                + `" autocomplete="off" class="layui-input" disabled>
				    </div>
				  </div>
				  <div class="layui-form-item">
				    <label class="layui-form-label">邮箱</label>
				    <div class="layui-input-block">
				      <input type="text" name="email" required  lay-verify="email" value="` +
                x.email
                + `" autocomplete="off" class="layui-input" disabled>
				    </div>
				  </div>
				  <div class="layui-form-item">
				    <label class="layui-form-label">角色</label>
				    <div class="layui-input-block">
				      <input type="text" name="rolename" required  lay-verify="role" value="` +
                x.role.rolename
                + `" autocomplete="off" class="layui-input" disabled>
				    </div>
				  </div>
				  <div class="layui-form-item">
				    <label class="layui-form-label">状态</label>
				    <div class="layui-input-block">` +
                stateStr
                + `</div>
				  </div>
				  <div class="layui-form-item layui-form-text">
				    <label class="layui-form-label">备注</label>
				    <div class="layui-input-block">
				      <textarea name="describle"  class="layui-textarea" disabled>
				      ` + x.describle + `
				      </textarea>
				    </div>
				  </div>
				</form>`
            });
            console.log(x.describle);
            form.render();
        }
        $scope.mod = function (x) {
            console.log(x);

            var list = "";

            for (var i = 0; i < $scope.roles.length; i++) {

                if ($scope.roles[i].roleid == x.role.roleid) {
                    var option = '<option selected value="' + $scope.roles[i].roleid + '">' + $scope.roles[i].rolename + '</option>';
                } else {
                    var option = '<option value="' + $scope.roles[i].roleid + '">' + $scope.roles[i].rolename + '</option>';

                }

                list = list + option;

            }
            console.log(list);

            var stateStr;
            if (x.state == '1') {
                stateStr = '<input type="radio" name="state" value="1" checked title="启用">  <input type="radio" name="state" value="0" title="禁用" >'
            } else {

                stateStr = '<input type="radio" name="state" value="1" title="启用" >  <input type="radio" name="state" value="0" title="禁用" checked >'
            }

            oldUserForm=layer.open({
                type: 1,
                title: "修改信息",
                area: ['640px', '600px'],
                content: `
				<form class="layui-form" id="layerform" action="" method="post">
				  <div class="layui-form-item">
				    <label class="layui-form-label">姓名</label>
				    <div class="layui-input-block">
				      <input type="text" name="username" required value="` + x.username + `" lay-verify="required" placeholder="请输入用户姓名" autocomplete="off" class="layui-input">
				    </div>
				  </div>
				    <div class="layui-form-item">
				     <label class="layui-form-label">职位</label>
				    <div class="layui-input-block">
				      <input type="text" name="position" value="` + x.position + `" required  lay-verify="required" placeholder="请输入用户姓名" autocomplete="off" class="layui-input">
				    </div>
				  </div>
				    <div class="layui-form-item">
				    <label class="layui-form-label">电话</label>
				    <div class="layui-input-block">
				      <input type="text" name="phonenumber" required value="` + x.phonenumber + `" lay-verify="phone" placeholder="请输入手机号" autocomplete="off" class="layui-input">
				    </div>
				  </div>
				  <div class="layui-form-item">
				    <label class="layui-form-label">邮箱</label>
				    <div class="layui-input-block">
				      <input type="text" name="email" required value="` + x.email + `" lay-verify="email" placeholder="请输入邮箱" autocomplete="off" class="layui-input">
				    </div>
				  </div>
				  <div class="layui-form-item">
				    <label class="layui-form-label">角色</label>
				    <div class="layui-input-block">
				      <select name="roleid" lay-verify="required">
				        <option value="">--请选择角色--</option>`
                + list +
                `    
				      </select>
				    </div>
				  </div>
				  <div class="layui-form-item">
				    <label class="layui-form-label">状态</label>
				    <div class="layui-input-block">
				      ` + stateStr + `
				    </div>
				  </div>
				  <div class="layui-form-item layui-form-text">
				    <label class="layui-form-label">备注</label>
				    <div class="layui-input-block">
				      <textarea name="describle" placeholder="请输入备注" class="layui-textarea">` + x.describle + `</textarea>
				    </div>
				  </div>
				  <div class="layui-form-item">
				    <div class="layui-input-block">
				      <button class="layui-btn" lay-submit lay-filter="mod">立即提交</button>
				      <input type="hidden" name="userid" value="` + x.userid + `">
				      <button type="reset" class="layui-btn layui-btn-primary">重置</button>
				    </div>
				  </div>
				</form>
				`
            })
            form.render();


        }
    })
});